/*============================================================================*/
/*                          The Freescale Cup                                 */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         apptask.c%
* %version:         1 %
* %created_by:      Continental %
* %date_created:     %
*=============================================================================*/
/* DESCRIPTION :  All tasks are called here                                   */
/*============================================================================*/
/* FUNCTION COMMENT : Add here your tasks, inside a periodic task             */
/* according to the desired execution period                                  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 22/08/2011  |  Revision                     | Continental      */
/*                                                                            */
/*============================================================================*/

/* Includes */
/* ------------------- */
/* Common declarations */
/* ------------------- */
#include "apptask.h"

/*       SW API    */
/* --------------- */
#include "HALInputs.h"
#include "HALOutputs.h"

/* Exported functions prototypes */
/* ----------------------------- */

 /**************************************************************
 *  Name                 : apptask_InitHW
 *	ID					 : TASK_INIT_HW
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_InitHW (void)
 {
	/* Do nothing */
 }
 
/**************************************************************
 *  Name                 : apptask_Lighting
 *	ID					 : TASK_LIGHTING
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_Lighting (void)
 {
	E_SM_LIGHTSEL_ST 	le_dbnc_LightingSelector;
	E_SM_AMBIENTLS_ST 	le_dbnc_AmbientLightSelector;
	le_dbnc_LightingSelector = Get_LightingSelector_State();
	
	switch (le_dbnc_LightingSelector)
	{
		case LS_OFF:
			Set_LowBeamLights_State 			(INACTIVE);
			/*Set_RightFrontParkingLights_State 	(INACTIVE);	60%
			Set_LeftFrontParkingLights_State 	(INACTIVE);*/
			Set_RightRearParkingLights_State 	(INACTIVE);
			Set_LeftRearParkingLights_State		(INACTIVE);
			break;
		case LS_DAYLIGHT:
			Set_LowBeamLights_State 			(INACTIVE);
			/*Set_RightFrontParkingLights_State 	(ACTIVE);	60%
			Set_LeftFrontParkingLights_State 	(ACTIVE);*/
			Set_RightRearParkingLights_State 	(ACTIVE);
			Set_LeftRearParkingLights_State		(ACTIVE);
			break;
		case LS_ON:
			Set_LowBeamLights_State 			(ACTIVE);
			/*Set_RightFrontParkingLights_State 	(ACTIVE);	60%
			Set_LeftFrontParkingLights_State 	(ACTIVE);*/
			Set_RightRearParkingLights_State 	(ACTIVE);
			Set_LeftRearParkingLights_State		(ACTIVE);
			break;
		case LS_AUTO:
			le_dbnc_AmbientLightSelector = Get_AmbientLightSelector_State();
			switch (le_dbnc_AmbientLightSelector)
			{
				/* Tony's code */
				case ALS_OFF:
					
					break;
				case ALS_HALF:
					
					break;
				case ALS_ON:
					
					break;
				default:
					break;
			}
			break;
		default:
			break;
	}
 }
 
 /**************************************************************
 *  Name                 : apptask_Road
 *	ID					 : TASK_ROAD
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_Road (void)
 {
	E_SM_INPUTS_ST le_dbnc_IngnitionSwitch;
	E_SM_INPUTS_ST le_dbnc_HeadlightsBeamModeBtn;
	E_SM_INPUTS_ST le_dbnc_HeadlightsContinuousModeBtn;
	
	static E_SM_OUTPUTS_ST le_ContinuousMode = INACTIVE;
	
	le_dbnc_IngnitionSwitch = Get_IgnitionSwitch_State();
	le_dbnc_HeadlightsBeamModeBtn = Get_HeadlightsBeamModeBtn_State();
	le_dbnc_HeadlightsContinuousModeBtn = Get_HeadlightsContinuousModeBtn_State();
	
	if(le_dbnc_IgnitionSwitch == VALID)
	{
		if((le_dbnc_HeadlightsBeamModeBtn == VALID) \
		&& (le_dbnc_HeadlightsContinuousModeBtn == INVALID))
		{
			ContinuousMode = INACTIVE;
			Set_HighBeamLights_State(ACTIVE);
		}
		else if((le_dbnc_HeadlightsBeamModeBtn == VALID) \
		&& (le_dbnc_HeadlightsContinuousModeBtn == VALID))
		{
			le_ContinuousMode = ACTIVE;
		}
		else if((le_dbnc_HeadlightsContinuousModeBtn == VALID) \
		&& (le_ContinuosMode == INVALID))
		{
			le_ContinuousMode = ACTIVE;
		}
		else if((le_dbnc_HeadlightsContinuousModeBtn == INVALID) \
		&& (le_dbnc_HeadlightsBeamModeBtn == VALID))
		{
			le_ContinuousMode = INACTIVE;
			Set_HighBeamLights_State(ACTIVE);
		}
		else if((le_dbnc_HeadlightsContinuousModeBtn != VALID) \
		&& (ContinuousMode == ACTIVE))
		{
			Set_HighBeamLights_State(INACTIVE);
			le_ContinuousMode = INACTIVE;
		}
		else if(le_dbnc_HeadlightsBeamModeBtn == INVALID)
		{
			Set_HighBeamLights_State(INACTIVE);
			le_ContinuousMode = INACTIVE;
		}
		else
		{
			Set_HighBeamLights_State(INACTIVE);
			le_ContinuousMode = INACTIVE;
		}
	}
	else
	{
		Set_HighBeamLights_State(INACTIVE);
		le_ContinuousMode = INACTIVE;
	}

	
	switch (le_ContinuosMode)
	{
		case ACTIVE:
			Set_HighBeamLights_State(ACTIVE);
			break;
		case INACTIVE:
			Set_HighBeamLights_State(INACTIVE);
			break;
		default:
			break;
	}
 }
 
 /**************************************************************
 *  Name                 : apptask_Emergency
 *	ID					 : TASK_EMERGENCY
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_Emergency	(void)
 {

 }
 
 /**************************************************************
 *  Name                 : apptask_Reverse
 *	ID					 : TASK_REVERSE
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_Reverse (void)
 {
	E_SM_INPUTS_ST le_dbnc_IgnitionSwitch;
	E_SM_INPUTS_ST le_dbnc_ReverseSwitch;
	
	le_dbnc_IgnitionSwitch = Get_IgnitionSwitch_State();
	le_dbnc_ReverseSwitch = Get_ReverseSwitch_State();
	
	if((le_dbnc_IgnitionSwitch == VALID) \
		&& (le_dbnc_ReverseSwitch == VALID))
	{
		Set_ReverseLights_State(ACTIVE);
	}
	else if((le_dbnc_Ignition == INVALID) \
		|| (le_dbnc_ReverseSwitch == INVALID))
	{
		Set_ReverseLights_State(INACTIVE);
	}
	else
	{
		Set_ReverseLights_State(INACTIVE);
	}
 }
 
 /**************************************************************
 *  Name                 : apptask_Stop
 *	ID					 : TASK_STOP
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_Stop (void)
 { 
    E_SM_INPUTS_ST le_dbnc_StopButton;
	le_dbnc_StopButton = Get_StopButton_State();
	
	switch (le_dbnc_StopButton)
	{
		case VALID:
			/* right group */
			Set_RightRearTurnSignal_State	(ACTIVE);
			Set_RightStopLights_State 		(ACTIVE);
			Set_RightEmergencyLigths_State 	(ACTIVE);
			
			/* left group */
			Set_LeftRearTurnSignal_State  	(ACTIVE);
			Set_LeftStopLights_State 		(ACTIVE);
			Set_LeftEmergencyLights_State 	(ACTIVE);
			break;
		case INVALID:
			/* right group */
			Set_RightRearTurnSignal_State	(INACTIVE);
			Set_RightStopLights_State 		(INACTIVE);
			Set_RightEmergencyLigths_State 	(INACTIVE);
			
			/* left group */
			Set_LeftRearTurnSignal_State  	(INACTIVE);
			Set_LeftStopLights_State 		(INACTIVE);
			Set_LeftEmergencyLights_State 	(INACTIVE);
			break;
		default:
			break;
	}
 }
 
 /**************************************************************
 *  Name                 : apptask_FrontSteering
 *	ID					 : TASK_FRONT_STEERING
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_FrontSteering	(void)
 {
	E_SM_INPUTS_ST le_dbnc_LeftTurnButton;
    E_SM_INPUTS_ST le_dbnc_RightTurnButton;
	E_SM_STWHEEL_ST le_SteeringWheel_Position;
	
	le_dbnc_LeftTurnButton = Get_LeftTurnButton_State();
	le_dbnc_RightTurnButton = Get_RightTurnButton_State();
	le_SteeringWheel_Position = Get_SteeringWheel_State ();

	switch (le_dbnc_LeftTurnButton)
	{
		case VALID:
			if (le_dbnc_RightTurnButton == VALID || le_SteeringWheel_Position == SWL_LEFT)
			{
				le_dbnc_LeftTurnButton = INVALID;
			}
			else
			{
				Set_LeftFrontTurnSignal_State(ACTIVE);
			}
		break;
		
		case INVALID;
			/* left group */
			Set_LeftFrontTurnSignal_State(INACTIVE);
		break;
		
		default:
		break;
	}
	switch (le_dbnc_RightTurnButton)
	{
		case VALID:
			if (le_dbnc_LeftTurnButton == VALID || le_SteeringWheel_Position == SWL_RIGHT)
			{
				le_dbnc_RightTurnButton=INVALID;
			}
			else
			{
				Set_RightFrontTurnSignal_State(ACTIVE);
			}
		break;
		
		case INVALID;
			/* right group */
			Set_RightFrontTurnSignal_State(INACTIVE);
		break;
		
		default:
		break;
	}
 }
 
 /**************************************************************
 *  Name                 : apptask_RearSteering
 *	ID					 : TASK_REAR_STEERING
 *  Description          :
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  no
 **************************************************************/
 void apptask_RearSteering (void)
 {
    E_SM_INPUTS_ST le_dbnc_LeftTurnButton;
    E_SM_INPUTS_ST le_dbnc_RightTurnButton;
	E_SM_STWHEEL_ST le_SteeringWheel_Position;
	static T_UWORD luw_counter_TurnButton = 0;
	
	le_dbnc_LeftTurnButton = Get_LeftTurnButton_State();
	le_dbnc_RightTurnButton = Get_RightTurnButton_State();
	le_SteeringWheel_Position = Get_SteeringWheel_State ();

	switch (le_dbnc_LeftTurnButton)
	{
		case VALID:
			if (le_dbnc_RightTurnButton == VALID || le_SteeringWheel_Position == SWL_LEFT)
			{
				luw_counter_TurnButton = 0;
				le_dbnc_LeftTurnButton=INVALID;
			}
			else
			{
				if (luw_counter_TurnButton <= Time_RT_RTS)
				{
					Set_LeftRearTurnSignal_State  	(ACTIVE);
					Set_LeftStopLights_State 		(INACTIVE);
					Set_LeftEmergencyLights_State 	(INACTIVE);
				}
				else if (luw_counter_TurnButton <= Time_RT_STL)
				{
					Set_LeftRearTurnSignal_State  	(INACTIVE);
					Set_LeftStopLights_State 		(ACTIVE);
					Set_LeftEmergencyLights_State 	(INACTIVE);
				}
				else if (luw_counter_TurnButton <= Time_RT_EL)
				{
					Set_LeftRearTurnSignal_State  	(INACTIVE);
					Set_LeftStopLights_State 		(INACTIVE);
					Set_LeftEmergencyLights_State 	(ACTIVE);
					luw_counter_TurnButton = 0;
				}
				else
				{
				}
			}
		break;
		
		case INVALID;
			/* left group */
			Set_LeftRearTurnSignal_State  	(INACTIVE);
			Set_LeftStopLights_State 		(INACTIVE);
			Set_LeftEmergencyLights_State 	(INACTIVE);
		break;
		
		default:
		break;
	}
	switch (le_dbnc_RightTurnButton)
	{
		case VALID:
			if (le_dbnc_LeftTurnButton == VALID || le_SteeringWheel_Position == SWL_RIGHT)
			{
				luw_counter_TurnButton = 0;
				le_dbnc_RightTurnButton=INVALID;
			}
			else
			{
				if (luw_counter_TurnButton <= Time_RT_RTS)
				{
					Set_RightRearTurnSignal_State  	(ACTIVE);
					Set_RightStopLights_State 		(INACTIVE);
					Set_RightEmergencyLights_State 	(INACTIVE);
				}
				else if (luw_counter_TurnButton <= Time_RT_STL)
				{
					Set_RightRearTurnSignal_State  	(INACTIVE);
					Set_RightStopLights_State 		(ACTIVE);
					Set_RightEmergencyLights_State 	(INACTIVE);
				}
				else if (luw_counter_TurnButton <= Time_RT_EL)
				{
					Set_RightRearTurnSignal_State  	(INACTIVE);
					Set_RightStopLights_State 		(INACTIVE);
					Set_RightEmergencyLights_State 	(ACTIVE);
					luw_counter_TurnButton = 0;
				}
				else
				{
				}
			}
		break;
		
		case INVALID;
			/* right group */
			Set_RightRearTurnSignal_State	(INACTIVE);
			Set_RightStopLights_State 		(INACTIVE);
			Set_RightEmergencyLigths_State 	(INACTIVE);
		break;
		
		default:
		break;
	}
 }
 